Explain how to copy an array into another in Go Programming Language | Go Programming Language
In this article, you will learn how to copy an array into another array in the Go Programming Language.
Arrays in GoLang are similar to other existing programming languages. In the Program, the programmer looks for a data type that can store multiple values of the same data type for that array data type. An array is a fixed-length data type that is used to store homogeneous elements in the memory. Due to the fixed length feature, they are not quite as popular in the GoLang language as a slice. In an array, the user is allowed to store zero or can store more than zero elements in it. The elements of the array are called by using the index. The index of the array starts with 0 and ends with the size-1. GoLang does not give a specific built-in function to copy one array into another array. But the user can create a copy of an array by simply assigning an array to a new variable by value or by reference.
If we create a copy of an array using by value and it makes some changes in the value of the original array, then it does not reflect in the copy of that array. And if we create a copy of an array using by reference and make some changes in the value of the original array, then it will reflect the changes in the copy of that array.
Syntax:
// creating a copy of an array using by value
arr:= arr1
// creating a copy of an array using by reference
arr:= &arr1
Example:
package mainimport "fmt" func main() { // Creating and initializing an arrayUsing shorthand declaration arr_1 := [5]string{"HTML", "CSS", "Java", "Python", "Go"} // Copying the array into new variable // Here, the elements are passed by value arr_2 := arr_1 fmt.Println("Array1: ", arr_1) fmt.Println("Array2:", arr_2) my_arr1[0] = "C Lang" /* Here, when we copy an arrayinto another array by value then the changes made in original array do not reflect in the copy of that array */ fmt.Println("\nArray1: ", arr_1) fmt.Println("Array2: ", arr_2)}OUTPUT:
Array1: [HTML, CSS, Java, Python, Go]
Array2: [HTML, CSS, Java, Python, Go]
Array1: [HTML, CSS, Java, Python, Go]
Array2: [HTML, CSS, Java, Python, Go]


